<template>
  <div class="app-container">
    <div class="button-area">
      <el-button type="primary" @click="dialogFormVisible = true"
        >新增</el-button
      >
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="名称"> </el-table-column>
      <el-table-column prop="type" label="类型"> </el-table-column>
      <el-table-column prop="deptId" label="部门编号"> </el-table-column>
      <el-table-column prop="code" label="code"> </el-table-column>
      <el-table-column prop="remark" label="备注"> </el-table-column>
      <el-table-column prop="msg" label="msg"> </el-table-column>
      <el-table-column prop="action" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="handleClick(scope.row, 'delete')"
            type="text"
            size="small"
            >删除</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleClick(scope.row, 'edit')"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="仓库" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="仓库类型" required prop="type">
          <el-radio v-model="form.type" label="1">大仓库</el-radio>
          <el-radio v-model="form.type" label="2">科室仓库</el-radio>
        </el-form-item>
        <el-form-item label="仓库名称" required prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="关联部门"
          required
          prop="deptId"
          v-if="form.type !== '1'"
        >
          <el-input v-model="form.deptId" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReagentStore,
  newReagentStore,
  delReagentStore,
  editReagentStore,
} from '@/api/reagent/store'
export default {
  name: 'Store',
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        type: '1',
        name: '',
        deptId: '',
      },
      rules: {
        type: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
      },
      isEdit: false,
      editId: '',
    }
  },
  created() {
    this.getReagentStore()
  },
  methods: {
    getReagentStore() {
      const self = this
      listReagentStore().then((response) => {
        if (response && response.rows) {
          self.tableData = response.rows
        }
      })
    },
    submitForm(formName) {
      const self = this
      self.$refs[formName].validate((valid) => {
        if (valid) {
          if (self.isEdit) {
            editReagentStore({ ...self.form, id: self.editId }).then(
              (response) => {
                if (response && response.code === 200) {
                  self.dialogFormVisible = false
                  self.$message({
                    message: '操作成功',
                    type: 'success',
                  })
                  self.getReagentStore()
                }
              },
            )
          } else {
            newReagentStore(self.form).then((response) => {
              if (response && response.code === 200) {
                self.dialogFormVisible = false
                self.$message({
                  message: '操作成功',
                  type: 'success',
                })
                self.getReagentStore()
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClick(row, type) {
      const self = this
      if (type === 'delete') {
        self
          .$confirm('此操作将永久删除该仓库, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
          .then(() => {
            delReagentStore(row.id).then((response) => {
              if (response && response.code === 200) {
                self.$message({
                  message: '操作成功',
                  type: 'success',
                })
                self.getReagent()
              }
            })
          })
      } else if (type === 'edit') {
        self.isEdit = true
        self.editId = row.id
        self.form = {
          ...row,
        }
        self.dialogFormVisible = true
      }
    },
  },
}
</script>

<style></style>
